home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / which.cmm < prev    next >
Text File  |  1995-10-10  |  5KB  |  184 lines

  1. //********************************************************************
  2. //*** Which.cmm - Show when command would be executed by searching ***
  3. //*** ver.1       for command along path.                          ***
  4. //********************************************************************
  5.  
  6. #include "Netware.lib"
  7.  
  8. if( defined(_WINDOWS_) || defined(_NTWIN_) || defined(_NTCON_) )
  9. {
  10.   ExecuteOrder = { ".CMM", ".COM", ".EXE", ".BAT", ".PIF" };
  11. } else if( defined(_OS2_) )
  12. {
  13.   ExecuteOrder = { ".CMM", ".COM", ".EXE", ".CMD", ".BAT" };
  14. } else {
  15.   ExecuteOrder = { ".CMM", ".COM", ".EXE", ".BAT" };
  16. }
  17.  
  18.  
  19. Instructions()
  20. {
  21.    printf("\a\n")
  22.    puts(`Which - Determine which command in PATH will execute, and what order`)
  23.    puts(``)
  24.    puts(`SYNTAX: Which <CommandSpec>`)
  25.    puts(``)
  26.    puts(`WHERE: CommandSpec is any file spec.  If CommandSpec has no extension then`)
  27.    printf(`       will search executables in this order:`)
  28.    for ( i = 0; i <= GetArraySpan(ExecuteOrder); i++ )
  29.       printf(" %s",ExecuteOrder[i])
  30.    puts(``)
  31.    puts(`EXAMPLES: Which EDIT`)
  32.    puts(`          Which SESSION.C*`)
  33.    exit(EXIT_FAILURE);
  34. }
  35.  
  36. /* ---------------------------------------------------------------------- */
  37.  
  38.    #define HKEY_CLASSES_ROOT       0x80000000
  39.    #define HKEY_CURRENT_USER       0x80000001
  40.    #define HKEY_LOCAL_MACHINE      0x80000002
  41.    #define HKEY_USERS              0x80000003
  42.    #define HKEY_PERFORMANCE_DATA   0x80000004
  43.  
  44.    #define REG_SZ                       1
  45.  
  46. RegCloseKey(pHKey)
  47. {
  48.    return DynamicLink("ADVAPI32","RegCloseKey",STDCALL,pHKey);
  49. }
  50.  
  51. RegCreateKey(pHKey,pSubKeyStr,pGetHKey)
  52. {
  53.    lRet = DynamicLink("ADVAPI32","RegCreateKeyA",STDCALL,pHKey,pSubKeyStr,lGetHKey);
  54.    pGetHKey = lGetHKey;
  55.    return lRet;
  56. }
  57.  
  58. RegQueryValueEx(pHKey,pValueName,pGetType,pGetData,pGetDataSize)
  59. {
  60.    lRet = DynamicLink("ADVAPI32","RegQueryValueExA",STDCALL,
  61.                       pHKey,pValueName,0,lGType,NULL,lBufSize);
  62.    if ( !lRet ) {
  63.       BLObSize(pGetData,lBufSize);
  64.       lRet = DynamicLink("ADVAPI32","RegQueryValueExA",STDCALL,
  65.                          pHKey,pValueName,0,lGetType,pGetData,pointer(lBufSize));
  66.       pGetType = lGetType;
  67.       pGetDataSize = lBufSize;
  68.    }
  69.    return lRet;                     
  70. }
  71.  
  72. /* ---------------------------------------------------------------------- */
  73.  
  74. main(argc,argv)
  75. {
  76.    if ( argc != 2 )   Instructions();
  77.  
  78.    NameParts = SplitFileName(argv[1]);
  79.    if ( NameParts.dir[0] ) {
  80.       printf("\aWHICH will not work with directory specifications.\n");
  81.       exit(EXIT_FAILURE);
  82.    }
  83.  
  84.    // if extension then use it, else use our extension list
  85.    if ( NameParts.ext[0] ) {
  86.       Extensions[0] = NameParts.ext;
  87.       ExtensionCount = 1;
  88.    } else {
  89.       Extensions = ExecuteOrder;
  90.       ExtensionCount = 1 + GetArraySpan(ExecuteOrder);
  91.    }
  92.    // get prioritized list of all directories
  93.    PathCount = BuildPathList(PathList);
  94.  
  95.    // for each directory, find all of the matching files
  96.    FindCount = 0;
  97.    for ( PathIdx = 0; PathIdx < PathCount; PathIdx++ ) {
  98.       for ( ExtensionIdx = 0; ExtensionIdx < ExtensionCount; ExtensionIdx++ ) {
  99.          FindCount += FindFileInDirectory(PathList[PathIdx],NameParts.name,Extensions[ExtensionIdx]);
  100.       }
  101.    }
  102.  
  103.    if ( !FindCount ) {
  104.       printf("\aNo commands found for %s\n",argv[1]);
  105.       exit(EXIT_FAILURE);
  106.    }
  107.  
  108. }
  109.  
  110. Count = 0;
  111. List[0] = "";
  112. strcpy(List[Count++],FullPath("."));
  113.  
  114. AddDir(dir)
  115. {
  116.   for( i=0;i<Count;i++ )
  117.     if( !stricmp(List[i],dir) ) return;   // Already looking there
  118.   strcpy(List[Count++],dir);
  119. }
  120.  
  121. BuildPathList(List1)
  122. {
  123.    // get full path of each directory from the PATH variable
  124.    if ( Path = getenv("PATH") ) {
  125.       for ( Dir = strtok(Path,";"); Dir; Dir = strtok(NULL,";") ) {
  126.          if ( Dir[0] ) AddDir(Dir);
  127.       }
  128.    }
  129.  
  130.    if( defined(_OS2_) || defined(_DOS_) || defined(_DOS32_) || defined(_NTCON_) )
  131.       {
  132.          if ( Path = getenv("CMMPATH") ) {
  133.             for ( Dir = strtok(Path,";"); Dir; Dir = strtok(NULL,";") ) {
  134.                if ( Dir[0] ) AddDir(Dir);
  135.             }
  136.          }
  137.       }
  138.    if( defined(_WINDOWS_) )
  139.      {
  140.        buffer = ""; SetArraySpan(buffer,100);
  141.        DynamicLink("KERNEL","GETPROFILESTRING",SWORD16,PASCAL,
  142.                    "CEnvi","CMMPATH","",buffer,100);
  143.        if( buffer[0] )
  144.          {
  145.            for ( Dir = strtok(buffer,";"); Dir; Dir = strtok(NULL,";") ) {
  146.              if ( Dir[0] ) AddDir(Dir);
  147.            }
  148.          }
  149.      }
  150.    if( defined(_NTWIN_) )
  151.      {
  152.        Path = ""; SetArraySpan(Path,200);
  153.        RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\Nombas\\CEnviWNT",lKey);
  154.        RegQueryValueEx(lkey,"CMMPATH",REG_SZ,Path,200);
  155.        RegCloseKey(lkey);
  156.  
  157.        for ( Dir = strtok(Path,";"); Dir; Dir = strtok(NULL,";") ) {
  158.          if ( Dir[0] ) AddDir(Dir);
  159.        }
  160.       }
  161.  
  162.    // make sure each element in path list ends in '\'
  163.    for ( i = 0; i < Count; i++ ) {
  164.       if ( strcmp(":\\",List[i]+1) )
  165.          strcat(List[i],"\\");
  166.    }
  167.  
  168.    List1 = List;
  169.  
  170.    return Count;
  171. }
  172.  
  173. FindFileInDirectory(DirSpec,NameSpec,ExtSpec)
  174. {
  175.    sprintf(FindSpec,"%s%s%s",DirSpec,NameSpec,ExtSpec);
  176.    if ( !(FindList = Directory(FindSpec,False,_A_RDONLY|_A_ARCH)) )
  177.       return 0;
  178.  
  179.    Count = 1 + GetArraySpan(FindList);
  180.    for ( i = 0; i < Count; i++ )
  181.       printf("  %s\n",FindList[i].name);
  182.    return Count;
  183. }
  184.